package com.macro.mall.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.macro.mall.common.api.CommonPageResult;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.convert.PmsProductAttrCategoryConfigConvert;
import com.macro.mall.dao.pms.vo.PmsProductAttributeCategoryConfigVo;
import com.macro.mall.dto.PmsProductAttributeCategoryConfigParam;
import com.macro.mall.dto.PmsProductAttributeSetItemDto;
import com.macro.mall.dto.PmsProductAttributeParam;
import com.macro.mall.dto.ProductAttrInfoDto;
import com.macro.mall.model.pms.PmsProductAttributeSetDO;
import com.macro.mall.service.PmsProductAttributeSetService;
import com.macro.mall.service.PmsProductAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Controller
@Api(tags = "PmsProductAttributeController")
@Tag(name = "PmsProductAttributeController", description = "商品属性管理")
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @Autowired
    private PmsProductAttributeService productAttributeService;

    @Autowired
    private PmsProductAttributeSetService pmsProductAttributeSetService;

    @Autowired
    private PmsProductAttrCategoryConfigConvert pmsProductAttrCategoryConfigConvert;

    @ApiOperation("根据分类查询属性列表或参数列表")
    @RequestMapping(value = "/listBySetId", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPageResult<PmsProductAttributeSetItemDto>> listBySetId(@RequestBody PmsProductAttributeParam param) {
        Assert.isTrue(CollectionUtil.isNotEmpty(param.getSetIdList()), "属性集合不能为空");
        if(param.getPageNum() == null){
            param.setPageNum(1);
        }
        if(param.getPageSize() == null){
            param.setPageSize(10);
        }
        List<PmsProductAttributeSetItemDto> productAttributeSetItemList = productAttributeService.getList(param.getSetIdList(), param.getType(), param.getPageSize(), param.getPageNum());
        return CommonResult.success(CommonPageResult.restPage(productAttributeSetItemList));
    }

    @ApiOperation("某几个属性分类下面的所有属性列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "type", value = "0表示属性，1表示参数", required = true, paramType = "query", dataType = "integer")})
    @RequestMapping(value = "/listAllAttr", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<PmsProductAttributeCategoryConfigVo>> listAllAttr(@RequestBody PmsProductAttributeCategoryConfigParam param) {
        Assert.isTrue(CollectionUtil.isNotEmpty(param.getAttributeSetIdList()), "属性分类不能为空");
        List<PmsProductAttributeSetItemDto> productAttributeList = productAttributeService.getAllList(param.getAttributeSetIdList());
        List<PmsProductAttributeCategoryConfigVo> productAttributeCategoryConfigVoList = pmsProductAttrCategoryConfigConvert.toVoList(productAttributeList);
        if (productAttributeCategoryConfigVoList.size() > 0) {
            List<Long> attrCategoryIdList = productAttributeCategoryConfigVoList.stream().map(PmsProductAttributeCategoryConfigVo::getAttributeSetId).collect(Collectors.toList());
            List<PmsProductAttributeSetDO> list = pmsProductAttributeSetService.listByIds(attrCategoryIdList);
            productAttributeCategoryConfigVoList.forEach(item -> {
                PmsProductAttributeSetDO productAttributeCategoryDO = list.stream().filter(pmsProductAttributeCategoryDO -> pmsProductAttributeCategoryDO.getId().equals(item.getAttributeSetId())).findFirst().orElse(null);
                item.setAttrCategoryName(productAttributeCategoryDO.getName());
            });
        }
        return CommonResult.success(productAttributeCategoryConfigVoList);
    }

    @ApiOperation("添加商品属性信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PmsProductAttributeParam productAttributeParam) {
        int count = productAttributeService.create(productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改商品属性信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@PathVariable Long id, @RequestBody PmsProductAttributeParam productAttributeParam) {
        int count = productAttributeService.update(id, productAttributeParam);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询单个商品属性")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsProductAttributeSetItemDto> getItem(@PathVariable Long id) {
        PmsProductAttributeSetItemDto productAttribute = productAttributeService.getItem(id);
        return CommonResult.success(productAttribute);
    }

    @ApiOperation("批量删除商品属性")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = productAttributeService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据商品分类的id获取商品属性及属性分类")
    @RequestMapping(value = "/attrInfo/{productCategoryId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<ProductAttrInfoDto>> getAttrInfo(@PathVariable Long productCategoryId) {
        List<ProductAttrInfoDto> productAttrInfoList = productAttributeService.getProductAttrInfo(productCategoryId);
        return CommonResult.success(productAttrInfoList);
    }
}
